package com.zzusedating.androidapp.adapters;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.zzusedating.androidapp.R;
import com.zzusedating.androidapp.UserInfoActivity;
import com.zzusedating.androidapp.model.BBSMessage;
import com.zzusedating.androidapp.tools.BasicCallback;
import com.zzusedating.androidapp.tools.ConstantsTools;
import com.zzusedating.androidapp.tools.MyApplication;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class BBSAdapter extends RecyclerView.Adapter<BBSAdapter.ViewHolder> {
    private ArrayList<BBSMessage> messages;
    private Context context;

    public BBSAdapter(Context context, ArrayList<BBSMessage> messages) {
        this.messages = messages;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).
                inflate(R.layout.recycler_item_bbs, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        viewHolder.setDate(messages.get(i));
        final int temp = i;
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, UserInfoActivity.class);
                intent.putExtra("userIDExtra", messages.get(temp).getUserId());
                context.startActivity(intent);
            }
        });
        viewHolder.deletView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyApplication.getClient().newCall(
                        new Request.Builder()
                                .get()
                                .url(String.format(
                                        Locale.CHINA, ConstantsTools.DELETE_MESSAGE, messages.get(temp).getMessageId()
                                        )
                                )
                                .build()
                ).enqueue(
                        new BasicCallback(
                                new Handler(new Handler.Callback() {
                                    @Override
                                    public boolean handleMessage(Message msg) {
                                        switch (msg.what) {
                                            case -1:
                                                MyApplication.Toast("未登录");
                                                break;
                                            case -2:
                                                MyApplication.Toast("权限不足");
                                                break;
                                            case 6:
                                                MyApplication.Toast("消息不存在");
                                                break;
                                            case 1:
                                                messages.remove(temp);
                                                notifyDataSetChanged();
                                                break;
                                        }
                                        return true;
                                    }
                                })
                        ) {
                            @Override
                            public void onResponse(Call call, @NonNull Response response) {
                                if (getStatus(response) == 1)
                                    getHandler().sendMessage(ConstantsTools.createMessage(1));
                                else
                                    getHandler().sendMessage(ConstantsTools.createMessage(getCode()));
                            }
                        });
            }
        });
    }

    @Override
    public int getItemCount() {
        return messages.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView nameView;
        TextView timeView;
        TextView contentView;
        ImageButton deletView;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            deletView = itemView.findViewById(R.id.RecyclerItemBBS_Delete);
            nameView = itemView.findViewById(R.id.RecyclerItemBBS_Name);
            contentView = itemView.findViewById(R.id.RecyclerItemBBS_Content);
            timeView = itemView.findViewById(R.id.RecyclerItemBBS_DateTime);
        }

        void setDate(BBSMessage message) {
            nameView.setText(message.getFromUser().getNickname());
            timeView.setText(DateFormat.getInstance().format(new Date(message.getSendTime())));
            contentView.setText(message.getContent());
        }
    }
}
